<?php
	$lama = mysql_fetch_array(mysql_query("select masa_berlaku from setting_waktu where id_setting = 1"));
	
	//Data Keluarga
	$id=$_POST['id'];
	$tgl=date("Y-m-d");
	$jenis=$_POST['Jenis'];
	$surat=$_POST['surat'];
	$masa_berlaku=tambah_tanggal($_POST['masa_berlaku'],$lama['masa_berlaku']);
	$foto=$_FILES['foto']['name'];
	
	//echo $masa_berlaku;
	$sesi=session_id();
	//echo $sesi;
	if($foto != ''){
		$acak=rand(0000,9999);
		$lokasi_file=$_FILES['foto']['tmp_name'];
		$nama_file1=$_FILES['foto']['name'];
		$nama_file=$acak.$nama_file1;
		move_uploaded_file($lokasi_file,"foto_peserta/"."$nama_file");
	}else{
		$asal='module/mod_peserta/js/tmp_foto/'.$sesi. '.jpg';
		$nama_file=rand(0000,9999)."_".$id.".jpg";
		$newfile='foto_peserta/'.$nama_file;
		$kopi_file=copy($asal, $newfile);
	}
	
	//Penomoran otomatis
	$tgl2 = date("my01");
	$query = "SELECT max(id_peserta) as maxID FROM peserta where id_peserta like '$tgl2%'";
	$kode = mysql_query($query);
	$data = mysql_fetch_array($kode);
	$idMax = $data['maxID'];
	$noUrut = (int) substr($idMax, 6, 4);
	$noUrut++;
	$newID = "$tgl2".sprintf("%04s", $noUrut);

	if(isset($_POST['submit'])){
		$sql="insert into peserta(id_peserta,id_jenis_peserta,id_penduduk,no_surat,tgl_mulai,masa_berlaku,foto) values ('$newID',$jenis,$id,'$surat','$tgl','$masa_berlaku','$nama_file')";
		$qry=mysql_query($sql);
	}
	
	$url_sukses="module/mod_peserta/cetak.php?id=$newID";
	if($qry){
		echo "<meta http-equiv='refresh' content='0; url=$url_sukses' target='_BLANK'>";
		exit;
	}else{
		echo mysql_error();
	}
?>